package com.sfeir.ergosoom.common.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Person implements Serializable {

    private static final long serialVersionUID = -5348634263603835324L;

    private String name;
    private String firstname;
    private String nickname;
    private String birthDay;
    private GeoLocalisation geo;
    private String title;
    private String aboutMe;
    private String company;
    private String url;
    private String photo;

    private List<Email> emails;
    private List<Address> addresses;
    private List<Tel> tels;
    private List<NetworkItem> networkItems;

    public Person() {
        geo = new GeoLocalisation();
        emails = new ArrayList<Email>();
        addresses = new ArrayList<Address>();
        tels = new ArrayList<Tel>();
        networkItems = new ArrayList<NetworkItem>();
    }

    public Person(String firstname, String lastname) {
        this.firstname = firstname;
        this.name = lastname;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname == null ? firstname : firstname.trim();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? name : name.trim();
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname == null ? nickname : nickname.trim();
    }

    public String getBirthDay() {
        return birthDay;
    }

    public void setBirthDay(String birthDay) {
        this.birthDay = birthDay;
    }

    public GeoLocalisation getGeo() {
        return geo;
    }

    public void setGeo(GeoLocalisation geo) {
        this.geo = geo;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title == null ? title : title.trim();
    }

    public String getAboutMe() {
        return aboutMe;
    }

    public void setAboutMe(String aboutMe) {
        this.aboutMe = aboutMe;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public List<Email> getEmails() {
        return emails;
    }

    public void setEmails(List<Email> emails) {
        this.emails = emails;
    }

    public List<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }

    public List<Tel> getTels() {
        return tels;
    }

    public void setTels(List<Tel> tels) {
        this.tels = tels;
    }

    public List<NetworkItem> getNetworkItems() {
        return networkItems;
    }

    public void setNetworkItems(List<NetworkItem> networkItems) {
        this.networkItems = networkItems;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getFullName() {
        return this.firstname + " " + this.name;
    }

    public static String blob(Person p) {
        String blob = "";
        if (p.name == null)
            blob += "§";
        else
            blob += p.name.replaceAll("§", "") + "§";

        if (p.firstname == null)
            blob += "§";
        else
            blob += p.firstname.replaceAll("§", "") + "§";

        if (p.nickname == null)
            blob += "§";
        else
            blob += p.nickname.replaceAll("§", "") + "§";

        if (p.birthDay == null)
            blob += "§";
        else
            blob += p.birthDay.replaceAll("§", "") + "§";

        if (p.title == null)
            blob += "§";
        else
            blob += p.title.replaceAll("§", "") + "§";

        if (p.aboutMe == null)
            blob += "§";
        else
            blob += p.aboutMe.replaceAll("§", "") + "§";

        if (p.company == null)
            blob += "§";
        else
            blob += p.company.replaceAll("§", "") + "§";

        if (p.url == null)
            blob += "§";
        else
            blob += p.url.replaceAll("§", "") + "§";

        if (p.photo == null)
            blob += "§";
        else
            blob += p.photo.replaceAll("§", "") + "§";

        if (p.geo == null)
            blob += "§§";
        else
            blob += p.geo.getLatitude() + "§" + p.geo.getLongitude() + "§";

        blob += p.tels.size() + "§";
        for (Tel t : p.tels) {
            blob += t.getType() + "§" + t.getPhoneNumber().replaceAll("§", "") + "§";
        }
        blob += p.networkItems.size() + "§";
        for (NetworkItem n : p.networkItems) {
            blob += n.getNetworkItemType().replaceAll("§", "") + "§" + n.getNetworkItemLink().replaceAll("§", "") + "§";
        }
        blob += p.emails.size() + "§";
        for (Email m : p.emails) {
            blob += m.getTypeEmail() + "§" + m.getEmail().replaceAll("§", "") + "§";
        }
        blob += p.addresses.size() + "§";
        for (Address a : p.addresses) {
            blob += a.getType() + "§" + a.getFullValue().replaceAll("§", "") + "§";
        }
        return blob;
    }

    public static Person unblob(String blob) {
        Person p = new Person();
        String tmp;

        int indexstart = 0;
        int indexstop = 0;

        indexstop = blob.indexOf("§", indexstart);

        tmp = blob.substring(indexstart, indexstop);
        p.setName(tmp);

        indexstart = indexstop + 1;
        indexstop = blob.indexOf("§", indexstart);

        tmp = blob.substring(indexstart, indexstop);
        p.setFirstname(tmp);

        indexstart = indexstop + 1;
        indexstop = blob.indexOf("§", indexstart);

        tmp = blob.substring(indexstart, indexstop);
        p.setNickname(tmp);

        indexstart = indexstop + 1;
        indexstop = blob.indexOf("§", indexstart);

        tmp = blob.substring(indexstart, indexstop);
        p.setBirthDay(tmp);

        indexstart = indexstop + 1;
        indexstop = blob.indexOf("§", indexstart);

        tmp = blob.substring(indexstart, indexstop);
        p.setTitle(tmp);

        indexstart = indexstop + 1;
        indexstop = blob.indexOf("§", indexstart);

        tmp = blob.substring(indexstart, indexstop);
        p.setAboutMe(tmp);

        indexstart = indexstop + 1;
        indexstop = blob.indexOf("§", indexstart);

        tmp = blob.substring(indexstart, indexstop);
        p.setCompany(tmp);

        indexstart = indexstop + 1;
        indexstop = blob.indexOf("§", indexstart);

        tmp = blob.substring(indexstart, indexstop);
        p.setUrl(tmp);

        indexstart = indexstop + 1;
        indexstop = blob.indexOf("§", indexstart);

        tmp = blob.substring(indexstart, indexstop);
        p.setPhoto(tmp);

        indexstart = indexstop + 1;
        indexstop = blob.indexOf("§", indexstart);

        tmp = blob.substring(indexstart, indexstop);
        if (!tmp.equals(""))
            p.getGeo().setLatitude(Double.parseDouble(tmp));

        indexstart = indexstop + 1;
        indexstop = blob.indexOf("§", indexstart);

        tmp = blob.substring(indexstart, indexstop);
        if (!tmp.equals(""))
            p.getGeo().setLongitude(Double.parseDouble(tmp));

        indexstart = indexstop + 1;
        indexstop = blob.indexOf("§", indexstart);

        tmp = blob.substring(indexstart, indexstop);
        if (!tmp.equals("")) {
            int k = Integer.parseInt(tmp);
            for (int j = 0; j < k; j++) {
                Tel t = new Tel();
                indexstart = indexstop + 1;
                indexstop = blob.indexOf("§", indexstart);
                tmp = blob.substring(indexstart, indexstop);
                t.setType(Integer.parseInt(tmp));
                indexstart = indexstop + 1;
                indexstop = blob.indexOf("§", indexstart);
                tmp = blob.substring(indexstart, indexstop);
                t.setPhoneNumber(tmp);
                p.tels.add(t);
            }
        }

        indexstart = indexstop + 1;
        indexstop = blob.indexOf("§", indexstart);

        tmp = blob.substring(indexstart, indexstop);
        if (!tmp.equals("")) {
            int k = Integer.parseInt(tmp);
            for (int j = 0; j < k; j++) {
                NetworkItem ni = new NetworkItem();
                indexstart = indexstop + 1;
                indexstop = blob.indexOf("§", indexstart);
                tmp = blob.substring(indexstart, indexstop);
                ni.setNetworkItemType(tmp);
                indexstart = indexstop + 1;
                indexstop = blob.indexOf("§", indexstart);
                tmp = blob.substring(indexstart, indexstop);
                ni.setNetworkItemLink(tmp);
                p.networkItems.add(ni);
            }
        }

        indexstart = indexstop + 1;
        indexstop = blob.indexOf("§", indexstart);

        tmp = blob.substring(indexstart, indexstop);
        if (!tmp.equals("")) {
            int k = Integer.parseInt(tmp);
            for (int j = 0; j < k; j++) {
                Email e = new Email();
                indexstart = indexstop + 1;
                indexstop = blob.indexOf("§", indexstart);
                tmp = blob.substring(indexstart, indexstop);
                e.setTypeEmail(Integer.parseInt(tmp));
                indexstart = indexstop + 1;
                indexstop = blob.indexOf("§", indexstart);
                tmp = blob.substring(indexstart, indexstop);
                e.setEmail(tmp);
                p.emails.add(e);
            }
        }

        indexstart = indexstop + 1;
        indexstop = blob.indexOf("§", indexstart);

        tmp = blob.substring(indexstart, indexstop);
        if (!tmp.equals("")) {
            int k = Integer.parseInt(tmp);
            for (int j = 0; j < k; j++) {
                Address a = new Address();
                indexstart = indexstop + 1;
                indexstop = blob.indexOf("§", indexstart);
                tmp = blob.substring(indexstart, indexstop);
                a.setType(Integer.parseInt(tmp));
                indexstart = indexstop + 1;
                indexstop = blob.indexOf("§", indexstart);
                tmp = blob.substring(indexstart, indexstop);
                a.setFullValue(tmp);
                p.addresses.add(a);
            }
        }
        return p;
    }
}
